CompraProveedor = function(){
	var configWindow = {
		add: "compraProveedorAgregarTop",
		edit: "compraProveedorEditarTop",
		save: "compraProveedorGuardarTop",
		remove: "compraProveedorEliminarTop",
		grid: "compraProveedorGrid",
		form: "compraProveedorForm"
	};
	var configWindowBottom = {
			add: "compraProveedorAgregarBottom",
			edit: "compraProveedorEditarBottom",
			save: "compraProveedorGuardarBottom",
			remove: "compraProveedorEliminarBottom"
	};
	return {
		agregarCompraProveedor: function(){
			Asi.form.switchButton(configWindow, "add");
			Asi.form.switchButton(configWindowBottom, "add");
//			Ext.getCmp("compraProveedorFechaNacimiento").allowEnable = true;
		},
		editarCompraProveedor: function(){
//			Ext.getCmp("compraProveedorFechaNacimiento").allowEnable = false;
			Asi.form.switchButton(configWindow, "edit");
			Asi.form.switchButton(configWindowBottom, "edit");
		},
		eliminarCompraProveedor: function(){
			Asi.message.confirmDelete(function(){
				Asi.message.progress("Eliminando Informaci\u00F3n...");
				Ext.Ajax.request({
					timeout: Asi.constants.TIMEOUT_SECONDS,
					url: Asi.constants.CONTEXT_PATH + "/compraProveedor/delete",
					params: {
						compraProveedorId: Asi.utils.getValue("compraProveedorId")
					},
					callback: function(options, success, response){
						Ext.Msg.hide();
						if(success){
							var jsonObject = Asi.utils.ajaxRequestGetJson(response);
							if(jsonObject && jsonObject.success){
								Asi.form.switchButton(configWindow, "remove");
								Asi.form.switchButton(configWindowBottom, "remove");
								Asi.message.alert(jsonObject.message);
								Asi.form.clearAndDisable("compraProveedorForm");
								if(jsonObject.compraProveedors){
			    					Ext.getCmp("compraProveedorGrid").getStore().loadData(jsonObject.compraProveedors);
								}
								return;
							}
							jsonObject = jsonObject || {};
							Asi.message.alertInvalid(jsonObject.message || Asi.constants.DEFAULT_ERROR_MESSAGE);
							return;
						}
						Asi.message.alertInvalid(Asi.constants.DEFAULT_ERROR_MESSAGE);
					}
				});
			});
		},
		guardarCompraProveedor: function(){
			Asi.message.progress("Guardando Informaci\u00F3n...");
			Ext.getCmp("compraProveedorForm").getForm().submit({
    			url: Asi.constants.CONTEXT_PATH + "/empleado/compraProveedor/save",
    			timeout: Asi.constants.TIMEOUT_SECONDS,
    			params: {
    				uploadFile: Asi.utils.getValue("uploadFile"),
    				proveedorId: Ext.getCmp("ctgProveedorId").value
//    				"compra.kycPersonaJuridica.kycPersonaJuridicaId": AsiKYC.getKycPersonaJuridicaId()
    			},
    			success: function(form, action){
    				Asi.form.switchButton(configWindow, "save");
    				Asi.form.switchButton(configWindowBottom, "save");
    				Asi.message.alert(action.result.message);
    				Asi.form.setDisable("compraProveedorForm", true);
    				if(action.result.compras){
    					Ext.getCmp("compraProveedorGrid").getStore().loadData(action.result.compras);
//    					Ext.getCmp("compraProveedorGrid").getSelectionModel().select(action.result.compraProveedorIndex);
    				}
    			},
    			failure: Asi.form.failureProcedure
   			});
		},
		init: function(config){
			var compraProveedors = config.compraProveedors;
			var configToReturn = {};
			var storeMaterial = new Ext.data.SimpleStore({
				data: config.ctgColor || [],
				fields: [
				         "ctgCatalogoId",
				         "ctgCatalogoNombre",
				         "ctgCatalogoPadre",
				         "ctgCatalogoHijo"
				        ]
			});
			configToReturn.items = [];
			configToReturn.menu = AsiBotones.getBotones(AsiGen.getTipoUsuario(), AsiGen.getPantallaId());
			configToReturn.items.push({
				flex: 1,
				title: "COMPRA A PROVEEDOR",
				autoScroll: true,
				xtype: "panel",
				layout: {
				    type: "vbox",
				    align : "center",
				    pack  : "start"
				},
				defaults: {width: 780, margins: "5 0 5 0"},
				dockedItems: [
					{
						xtype: "toolbar",
						dock: "top",
//						hidden: AsiKYC.getKycVigente() === false,
						items: [
						   '->',
						   {
							   text: "Nuevo",
							   id: "compraProveedorAgregarTop",
							   iconCls: Asi.constants.icons.ADD_ICON,
							   handler: CompraProveedor.agregarCompraProveedor
						   },{
					    	   text: "Editar",
					    	   id: "compraProveedorEditarTop",
							   iconCls: Asi.constants.icons.EDIT_ICON,
							   handler: CompraProveedor.editarCompraProveedor,
					       },{
					    	   text: "Guardar",
					    	   id: "compraProveedorGuardarTop",
							   iconCls: Asi.constants.icons.SAVE_ICON,
							   handler: CompraProveedor.guardarCompraProveedor
					       },{
					    	   text: "Eliminar",
					    	   id: "compraProveedorEliminarTop",
							   iconCls: Asi.constants.icons.DELETE_ICON,
							   handler: CompraProveedor.eliminarCompraProveedor
					       }
					    ]
					},{
			        	xtype: "toolbar",
			        	dock: "bottom",
//			        	hidden: AsiKYC.getKycVigente() === false,
			        	bodyStyle: "border: solid",
			        	items: [
			        	   '->',
			        	   {
							   text: "Nuevo",
							   id: "compraProveedorAgregarBottom",
							   iconCls: Asi.constants.icons.ADD_ICON,
							   handler: CompraProveedor.agregarCompraProveedor
						   },{
					    	   text: "Editar",
					    	   id: "compraProveedorEditarBottom",
							   iconCls: Asi.constants.icons.EDIT_ICON,
							   handler: CompraProveedor.editarCompraProveedor
					       },{
					    	   text: "Guardar",
					    	   id: "compraProveedorGuardarBottom",
							   iconCls: Asi.constants.icons.SAVE_ICON,
							   handler: CompraProveedor.guardarCompraProveedor
					       },{
					    	   text: "Eliminar",
					    	   id: "compraProveedorEliminarBottom",
							   iconCls: Asi.constants.icons.DELETE_ICON,
							   handler: CompraProveedor.eliminarCompraProveedor
					       }
		        	    ]
			        }
	            ],
				items: [
					{
						xtype: "grid",
						id: "compraProveedorGrid",
						height: 150,
						minHeight: 150,
						collapsible : true,
						store: new Ext.data.SimpleStore({
					    	data: config.compras || [],
					    	fields: [
								"compraId",
								"compraFecha",
								"compraTotal",
								"compra_imagen",
								"usuarioId",
								"usuarioLogin",
								"ctgProveedorId",
								"ctgProveedorNombre",
								"ctgProveedorDireccion"
			    	        ]
					    }),
					    columns: [
				            {header: "Compra Id",  dataIndex: "compraId", flex:1, minWidth: 120},
				            {header: "Proveedor",  dataIndex: "ctgProveedorNombre", flex:1, minWidth: 120},
				            {header: "Fecha de Compra",  dataIndex: "compraFecha", flex:1, width: 120},
				            {header: "Total",  dataIndex: "compraTotal", align: "right", flex:1, width: 40}
				            

					    ],

					    listeners: {
					    	selectionchange: function(model, records){
					    		if(!records || records.length <= 0) return;
					    		var record = records[0];
					    		if(record != null){

					    			Asi.form.setValues("compraProveedorForm", record.data);
					    			Asi.form.setDisable("compraProveedorForm");
					    		}
					    		Asi.form.switchButton(configWindow, "rowclick");
					    		Asi.form.switchButton(configWindowBottom, "rowclick");
					    	},
					    	afterrender: function(){
					    		if(this.store.data.items.length > 0) Asi.grid.selectFirstRow(this);
					    		Asi.form.switchButton(configWindow, "cancelNotClear");
					    		Asi.form.switchButton(configWindowBottom, "cancelNotClear");
					    	}
					    }
					},{
			        	xtype: "form",
						id: "compraProveedorForm",
						flex: 1,
					    border: false,
					    autoScroll: true,
						layout: {
							type: "table",
							columns: 6,
							style : {
								width : "730px",
								"margin-top" : "5px",
								"margin-bottom" : "40px"
							},
							align : "center"
						},
//						buttons : [ {
//							text : "Buscar",
//							handler : Menu.digitalizarFacturaCompra
//						} ],
						defaults: {
							width: 230,
							selectOnFocus: true,
							enforceMaxLength: true,
							maxLength: 200,
							typeAhead: true,
							minChars: 1,
							queryMode: "local",
							forceSelection: true,
							allowEnable: true,
							colspan: 2
						},
						items: [
				        	 {
								xtype: "label",
								text: "DATOS DE COMPRAS",
								cls: "x-form-item label_header",
								colspan: 8,
								width: 730
							},
							{xtype: "label", text: "Proveedor", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "Fecha", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "Total", cls: "x-form-item label_spacing"},
							{
								xtype: "combo",
								id: "ctgProveedorId",
								name: "compra.ctgProveedorId",
								store: new Ext.data.SimpleStore({
									data: config.proveedores || [],
									fields: [
									         "ctgCatalogoId",
									         "ctgCatalogoNombre"
									        ]
								}),
								displayField: "ctgCatalogoNombre",
								valueField: "ctgCatalogoId",
								allowBlank: false
							},{
								xtype: "datefield",
								id: "compraFecha",
								name: "compra.compraFecha"
							},{
								xtype: "numberfield",
								id: "compraTotal",
								name: "compra.compraTotal",
								maxLength: 100
							},{
							    xtype: "filefield",
							    id: "uploadFile",
							    name: "uploadFile",
							    fieldLabel: "Comprobante de Compra",
							    width: 265,
							    buttonText: "",
							    tooltip: "Seleccionar Archivo",
							    buttonConfig: {
						    	iconCls: Asi.constants.icons.OPEN_ICON,
						    	tooltip: "Seleccionar Comprobante de Compra"
				    		}},{
									xtype: "hidden",
									id: "compraId",
									name: "compra.compraId"
							}

						]
			        }
				]
				
			});
			return configToReturn;
		}
	};
}();